<?php
if (!(defined('IN_IA'))) 
{
	exit('Access Denied');
}
class Index_Page extends WebPage
{
    public function type() 
    {
        global $_W,$_GPC;
        $uniacid=$_W['uniacid'];
        load()->func('tpl');
        $pindex = max(1, intval($_GPC['page']));
        if(!$pindex){
        	$pindex=1;
        }
        $num=8;
        $start=($pindex - 1) * $num;
        $sql="select * from ".tablename('bd_cms_category')." where `uniacid`='$uniacid' order by cat_sort desc LIMIT " .$start.','.$num;
        $list=pdo_fetchall($sql);
        $total = pdo_fetchcolumn("select count(*) from ".tablename('bd_cms_category')." where `uniacid`='$uniacid'");
        $pager = pagination($total, $pindex, $psize);
        include $this->template();
    }

    public function type_add() {
        global $_W,$_GPC;
        $uniacid=$_W['uniacid'];
        $uid=$_W['uid'];
        if($_POST){
            $data=array();
            $data['cat_sort']=$_POST['cat_sort'];
            $data['cat_logo']=$_POST['cat_logo'];
            $data['cat_name']=$_POST['cat_name'];
            $data['uniacid']=$uniacid;
            $data['uid']=$uniacid;
            $data['add_time']=date('Y-m-d H:i:s');
            $data['last_time']=date('Y-m-d H:i:s');
            $res=pdo_insert('bd_cms_category',$data);  
            if($_POST['jixu']){
                //继续添加
                $this->success('添加成功','',2);
            }else{
                $this->success('添加成功','new/index/type');
            }
        }
        load()->func('tpl');
        include $this->template();
    }
    /*
    * 修改分类
     */
    public function type_edit(){
        global $_W,$_GPC;
        $id=$_GET['cat_id'];
        $uniacid=$_W['uniacid'];
        $category=pdo_get('bd_cms_category',array('cat_id'=>$id,'uniacid'=>$uniacid));
        if($_POST['cat_id']){
            $data=array();
            $data['cat_sort']=$_POST['cat_sort'];
            $data['cat_logo']=$_POST['cat_logo'];
            $data['cat_name']=$_POST['cat_name'];
            $data['uniacid']=$uniacid;
            $data['uid']=$uniacid;
            $data['add_time']=date('Y-m-d H:i:s');
            $data['last_time']=date('Y-m-d H:i:s');
            $res=pdo_update('bd_cms_category',$data,array('cat_id'=>$id));
            $this->success('修改成功','new/index/type');
        }
        load()->func('tpl');
        include $this->template();
    }

    /*
    * 删除分类
     */

    public function type_del(){
        global $_W,$_GPC;
        $uniacid=$_W['uniacid'];
        if($_GPC['cat_id']){
            $id=$_GPC['cat_id'];
            //判断分类下有没有文章
            $has=pdo_get('bd_cms_article',array('article_cat'=>$id,'uniacid'=>$uniacid));
            if($has){
            	$this->error('该分类下有对应文章','new/index/type');
            }else{
            	$data=pdo_delete('bd_cms_category',array('cat_id'=>$id,'uniacid'=>$uniacid));
            	$this->success('删除成功','new/index/type');
            }

        }
    }
/******************************文章咨询首页*************************/
	public function index(){
		
       global $_W,$_GPC;
        $uniacid=$_W['uniacid'];
        $title=$_GPC['article_title'];
        $where='';
        if($title!=''){
            $where = "and article_title like'%".$title."%'" ;
        }        
        $pindex = max(1, intval($_GPC['page']));
        $psize = 8;
        $sql="select a.*,c.cat_name from ".tablename('bd_cms_article')."  as a Left Join" .tablename('bd_cms_category')." as c On a.article_cat=c.cat_id where a.`uniacid`='$uniacid' $where order by a.article_sort desc LIMIT " . ($pindex - 1) * $psize . ",{$psize}";
        $list=pdo_fetchall($sql);
        $total = pdo_fetchcolumn("select count(*) from ".tablename('bd_cms_article')."  as a Left Join" .tablename('bd_cms_category')." as c On a.article_cat=c.cat_id where a.`uniacid`='$uniacid' $where order by a.article_sort desc");
        $pager = pagination($total, $pindex, $psize);
        $i = ($pindex - 1) * $psize+1;

        include $this->template();
	}

    /****************批量删除********************************/
	public function new_editall(){
		global $_W,$_GPC;
		$uniacid=$_W['uniacid'];
		if(empty($_GPC['xuanze'])){
            $this->error('未选择数据','',0);
        }
		if($_GPC['piliang'] == 1){
                    //批量删除
			foreach ($_GPC['xuanze'] as $key => $value) {
				$data['article_id']=$value;
                $data['uniacid'] = $uniacid;
                pdo_delete('bd_cms_article',$data);
             }
             $this->success('删除成功','',2);
		}
	}

	/****************添加文章********************************/
    public function new_add() {
        global $_W,$_GPC;
        $uniacid=$_W['uniacid'];
        $uid=$_W['uid'];
        load()->func('tpl');
        
        $jixu=$_GPC['jixu'];
        if($_W['ispost']){
            $data=array();
            $data['article_thumb']=$_GPC['article_thumb'];
            $data['article_title']=$_GPC['article_title'];
            $data['article_cat']=$_GPC['article_cat'];
            $data['article_author']=$_GPC['article_author'];
            $data['article_clicks']=$_GPC['article_clicks'];
            $data['is_show']=$_GPC['is_show'];
            $data['article_sort']=$_GPC['article_sort'];
            $data['article_desc']=$_GPC['article_desc'];
            $data['article_content']=$_GPC['article_content'];
            $data['uniacid']=$uniacid;
            $data['uid']=$uid;
            $data['add_time']=date("Y-m-d H:i:s");
            $data['last_time']=date("Y-m-d H:i:s");
            $res=pdo_insert('bd_cms_article',$data); 
            if(!empty($jixu)){
            	$this->success('添加成功','',2);
            }else{
            	$this->success('添加成功','new/index/index');
            }
        }
        $product_type=pdo_fetchall("select * from ".tablename('bd_cms_category')." where `uniacid`='$uniacid'");
        include $this->template();
	}
    /*
    * 修改资讯
     */
    public function new_edit(){
        global $_W,$_GPC;
        load()->func('tpl');
        $id=$_GPC['article_id'];
        $uniacid=$_W['uniacid'];
        $article=pdo_get('bd_cms_article',array('article_id'=>$id,'uniacid'=>$uniacid));

        if($_POST['article_id']){
            $data=array();
            $id=$_POST['article_id'];
            $data['article_thumb']=$_POST['article_thumb'];
            $data['article_title']=$_POST['article_title'];
            $data['article_cat']=$_POST['article_cat'];
            $data['article_author']=$_POST['article_author'];
            $data['article_clicks']=$_POST['article_clicks'];
            $data['is_show']=$_POST['is_show'];
            $data['article_sort']=$_POST['article_sort'];
            $data['article_desc']=$_POST['article_desc'];
            $data['article_content']=$_POST['article_content'];
            $data['last_time']=date("Y-m-d H:i:s");
            $res=pdo_update('bd_cms_article',$data,array('article_id'=>$id));
            $this->success('修改成功','new/index/index');
        }
        $product_type=pdo_getall('bd_cms_category',array('uniacid'=>$uniacid));
        include $this->template();

    }

    /*
    * 删除资讯
     */
    public function new_del(){
        global $_W,$_GPC;
        $uniacid=$_W['uniacid'];
        if($_GPC['article_id']){
            $id=$_GPC['article_id'];
            $data=pdo_delete('bd_cms_article',array('article_id'=>$id,'uniacid'=>$uniacid));
            $this->success('删除成功','new/index/index');
        }
    }
    /*
     * 去除富文本内的字体
     * 2018年1月19日13:22:00
     */
    public function fwb_del_font($str){
        $str = htmlspecialchars_decode($str);
        $re = preg_replace("/font-family:.*;/i",'',$str);
        $re = preg_replace("/font-family:.*\"/i",'"',$re);
        return $re;
    }

}

?>